/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.gp.profile;

import be.ugent.medirem.logic.GpRemote;
import be.ugent.medirem.persistence.entity.details.LocationDetails;
import be.ugent.medirem.persistence.entity.values.GpValues;
import be.ugent.medirem.persistence.entity.values.LocationValues;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import servlets.gp.patient.profile.EditPatientProfileServlet;

/**
 *
 * @author Roelie
 */
public class EditGpProfileServlet extends HttpServlet {

    @EJB
    private GpRemote gpBean;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getUserPrincipal().getName();
        Long gpId = gpBean.getGpId(username);
        boolean validate = true;

        GpValues p = (GpValues) request.getAttribute("gp");
        LocationValues l = (LocationValues) request.getAttribute("location");

        try {
            p.setId(gpId);

            gpBean.update(p);

            LocationDetails ld = gpBean.getLocation(gpId);
            if (ld == null) {
                gpBean.updateGpLocation(gpId, null, l);
            } else {
                gpBean.updateGpLocation(gpId, ld.getId(), l);
            }
        } catch (Exception ex) {
            Logger.getLogger(EditPatientProfileServlet.class.getName()).log(Level.SEVERE, null, ex);
            request.getSession().setAttribute("flash", "An error occured when updating your gp information!");
            validate = false;
        }

        if (validate) {
            response.sendRedirect(request.getContextPath() + "/gp/profile");
        } else {
            request.getRequestDispatcher(request.getContextPath() + "/gp/profile/edit").forward(request, response);
        }

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
